package com.cotasul.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.cotasul.negocio.excecoes.EmailException;

public class Ferramentas {
	
	public static void enviaEmail(String msg, String titulo, String destino)
			throws Exception {
		// quem estiver utilizando um SERVIDOR PROXY descomente essa parte e
		// atribua as propriedades do SERVIDOR PROXY utilizado
		/*
		 * props.setProperty("proxySet","true");
		 * props.setProperty("socksProxyHost","192.168.155.1"); // IP do
		 * Servidor Proxy props.setProperty("socksProxyPort","1080"); // Porta
		 * do servidor Proxy
		 */
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		// props.put("mail.smtp.host", "mail.cotasul.com.br");
		// props.put("mail.smtp.socketFactory.port", "25");
		// props.put("mail.smtp.socketFactory.class",
		// "javax.net.ssl.SSLSocketFactory");
		// props.put("mail.smtp.auth", "true");
		// props.put("mail.smtp.port", "25");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					// E-mail Remetente
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(
								"cotasultn@gmail.com", "cotasul2012");
					}
				});

		// session.setDebug(true);

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("Cotasul")); // Remetente
			Address[] toUser = null;
			toUser = InternetAddress.parse(destino);
			message.setRecipients(Message.RecipientType.TO, toUser);
			message.setSubject(titulo);// Assunto
			// MimeBodyPart bodyPartAnexo = new MimeBodyPart();
			MimeBodyPart bodyMessage = new MimeBodyPart();
			bodyMessage.setText(msg+"\n\nAcesse www.cotasul.com.br para ver.");
			// bodyPartAnexo.setDataHandler(new DataHandler(new
			// FileDataSource(osImpressao)));
			// bodyPartAnexo.setFileName(osImpressao.getName());
			Multipart mult = new MimeMultipart();
			// mult.addBodyPart(bodyPartAnexo);
			mult.addBodyPart(bodyMessage);
			message.setContent(mult);
			message.saveChanges();
			Transport.send(message);
			System.out.println("E-mail Enviado");
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new EmailException();
		}
	}

	public static Date formataData(String dataFormatar, String formato) {

		SimpleDateFormat df = new SimpleDateFormat(formato);
		Date data = null;
		try {
			data = new Date(df.parse(dataFormatar).getTime());
			df = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;

	}

	public static String formataData(long data, String formato) {

		SimpleDateFormat df = new SimpleDateFormat(formato);
		String dataFormatada = "";
		dataFormatada = df.format(new Date(data));
		return dataFormatada;
	}

	/**
	 * Retorna true caso a primeira data for antes da segunda.
	 * 
	 * @param data1
	 *            Primeira data a ser comparada com a segunda.
	 * @param data2
	 *            Segunda data para ser comparada com a primeira.
	 * @return Retorna true quando a data1 for antes da data2.
	 * */
	public static Boolean comparaDataMaior(Date data1, Date data2) {
		if(data1.before(data2)){
			return true;
		}else{
			return false;
		}
	}
	

}
